﻿using BattleFury.Lib.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BattleFury.Lib.Input
{
	public class GamePadManager
		: Manager
	{
		private readonly GamePadState[] _previous = new GamePadState[4];
		private readonly GamePadState[] _current = new GamePadState[4];

		public override void Update(GameTime gameTime)
		{
			if (!IsEnabled) return;

			UpdateState(PlayerIndex.One);
			UpdateState(PlayerIndex.Two);
			UpdateState(PlayerIndex.Three);
			UpdateState(PlayerIndex.Four);
		}

		private void UpdateState(PlayerIndex index)
		{
			_previous[(int)index] = _current[(int)index];
			_current[(int)index] = GamePad.GetState(index);
		}

		public bool IsButtonPressed(PlayerIndex index, Buttons button)
		{
			return _current[(int)index].IsButtonDown(button) && _previous[(int)index].IsButtonUp(button);
		}

		public bool IsButtonHeld(PlayerIndex index, Buttons button)
		{
			return _current[(int)index].IsButtonDown(button) && _previous[(int)index].IsButtonDown(button);
		}
	}
}